/**
 * @desc 参数化对象
 * @param {Object} data 待拼接对象
 * @returns {String} 参数化的字符串
 */
const param = function(data) {
    let args = '';
    for (let i in data) {
        if (args != '') {
            args += '&';
        }
        args += i + '=' + encodeURIComponent(data[i]);
    }
    return args;
}

/**
 * @desc 文本内容xss过滤，防止xss注入风险
 * @param {String} unsafe 待过滤的字符串
 * @return {String}  过滤后的字符串
 **/
const filterXSS = function(unsafe) {
    return str.replace(/</g, "&lt;")
                .replace(/>/g, "&gt;")
                .replace(/\"/g, "&quot;")
                .replace(/\'/g, "&apos;");
}


/**
 * @desc 格式化时间秒
 * @param {Integer} value 秒数
 * @param {Integer} type 格式化的类型
 *       1: 00:01:20; 
 *       2: 3秒   1分20秒     1小时30分10秒
 * @return {String} 格式化的字符串
 */
const formatSeconds = function (value, type = 1) {
    let second = parseInt(value);
    let minute = 0;
    let hour = 0;
    let result;
    if (type == 1) {
        if (second > 60) {
            minute = parseInt(second / 60);
            second = parseInt(second % 60);
            if (minute > 60) {
                hour = parseInt(minute / 60);
                minute = parseInt(minute % 60);
            }

            if (second < 10) {
                second = '0' + second;
            }
            result = "" + second;

            if (minute > 0) {
                if (minute < 10) {
                    minute = '0' + minute;
                }
                result = "" + minute + ":" + result;
            }

            if (hour > 0) {
                if (hour < 10) {
                    hour = '0' + hour;
                }
                result = "" + hour + ":" + result;
            }
        } else {
            if (second < 10) {
                second = '0' + second;
            }
            result = '00:' + second;
        }

        return result;

    } else if (type == 2) {

        if (second > 60) {
            minute = parseInt(second / 60);
            second = parseInt(second % 60);
            if (minute > 60) {
                hour = parseInt(minute / 60);
                minute = parseInt(minute % 60);
            }
        }
        let result = "" + (parseInt(second) < 10 ? '0' + parseInt(second) : parseInt(second)) + "秒";
        if (minute > 0) {
            result = "" + (parseInt(minute) < 10 ? '0' + parseInt(minute) : parseInt(minute)) + "分" + result;
        }
        if (hour > 0) {
            result = "" + (parseInt(hour) < 10 ? '0' + parseInt(hour) : parseInt(hour)) + "小时" + result;
        }
        return result;
    }
}

/**
 * @desc 格式化时间戳
 * @param {String} timestamp unix时间戳
 * @param {String} format 格式化字符串
 * @return {String}  参数的值
 **/
const formatTime = function(format, timestamp){
    let d = new Date(timestamp);
    let o = {
        "M+": d.getMonth() + 1, //month
        "d+": d.getDate(), //day
        "h+": d.getHours(), //hour
        "m+": d.getMinutes(), //minute
        "s+": d.getSeconds() //second
    }

    if (/(y+)/.test(format)) {
        format = format.replace(RegExp.$1, (d.getFullYear() + "").substr(4 - RegExp.$1.length));
        for (let k in o) {
            if (new RegExp("(" + k + ")").test(format)) {
                format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
            }
        }

    }
    return format;
}

/**
 * @desc 获取url中参数
 * @param {String} name 参数名称
 * @return {String}  参数的值
 **/
const getUrlParam = function (name) {  
    let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");  
    let r = window.location.search.substr(1).match(reg);  
    if (r != null) return unescape(r[2]); return null;  
}  


/**
 * @desc 设置cookie
 * @param {String} name 存储的键值
 * @param {String} value 值
 * @param {String} expiredays 过期时间
 **/
const setCookie = function(name, value, expiredays){
    let exdate=new Date()
    exdate.setDate(exdate.getDate()+expiredays)
    document.cookie = name+ "=" +escape(value)+
    ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString())
}

/**
 * @desc 获取cookie
 * @param {String} name cookie键值
 * @return {String} cookie对应的值
 **/
const getCookie = function(name){
    let arr, reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
    return (arr=document.cookie.match(reg)) ? unescape(arr[2]) : null;
}

/**
 * @desc 获取唯一uuid
 * @return {String} uuid
 **/
const getUUID = function (){ 
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {  
        var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);  
        return v.toString(16);  
    });  
}

const setTitle = function(title) {
    document.title = title;
    if (/ip(hone|od|ad)/i.test(navigator.userAgent)) {
        var i = document.createElement('iframe');
        i.src = '/favicon.ico';
        i.style.display = 'none';
        i.onload = function() {
            setTimeout(function(){
                i.remove();
            }, 9)
        }
        document.body.appendChild(i);
    }
}

export default {
    param,
    filterXSS,
    getUrlParam,
    formatSeconds,
    formatTime,
    setCookie,
    getCookie,
    getUUID,
    setTitle
}